﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JT.Data;
using JT.Domain.TimeLine;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;

namespace JT.WEB.Areas.JtSiteAdmin.Controllers
{
    [Area("JtSiteAdmin")]
    public class TimeLinesController : Controller
    {
        private readonly ContextBase _context;

        public TimeLinesController(ContextBase context)
        {
            _context = context;
        }

        // GET: TimeLines
        public async Task<IActionResult> Index()
        {
            Microsoft.EntityFrameworkCore.Query.IIncludableQueryable<TimeLine, Year> contextBase = _context.TimeLines.Include(t => t.YearNavigation);
            return View(await contextBase.ToListAsync());
        }

        // GET: TimeLines/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            TimeLine timeLine = await _context.TimeLines
                .Include(t => t.YearNavigation)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (timeLine == null)
            {
                return NotFound();
            }

            return View(timeLine);
        }

        // GET: TimeLines/Create
        public IActionResult Create()
        {
            ViewData["YearId"] = new SelectList(_context.Years, "YearId", "YearId");
            return View();
        }

        // POST: TimeLines/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,YearId,Months,Content")] TimeLine timeLine)
        {
            if (ModelState.IsValid)
            {
                _context.Add(timeLine);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            ViewData["YearId"] = new SelectList(_context.Years, "YearId", "YearId", timeLine.YearId);
            return View(timeLine);
        }

        // GET: TimeLines/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            TimeLine timeLine = await _context.TimeLines.FindAsync(id);
            if (timeLine == null)
            {
                return NotFound();
            }
            ViewData["YearId"] = new SelectList(_context.Years, "YearId", "YearId", timeLine.YearId);
            return View(timeLine);
        }

        // POST: TimeLines/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,YearId,Months,Content")] TimeLine timeLine)
        {
            if (id != timeLine.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(timeLine);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TimeLineExists(timeLine.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["YearId"] = new SelectList(_context.Years, "YearId", "YearId", timeLine.YearId);
            return View(timeLine);
        }

        // GET: TimeLines/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            TimeLine timeLine = await _context.TimeLines
                .Include(t => t.YearNavigation)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (timeLine == null)
            {
                return NotFound();
            }

            return View(timeLine);
        }

        // POST: TimeLines/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            TimeLine timeLine = await _context.TimeLines.FindAsync(id);
            _context.TimeLines.Remove(timeLine);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool TimeLineExists(int id)
        {
            return _context.TimeLines.Any(e => e.Id == id);
        }
    }
}
